<?php

/**
 * This is the model class for table "xyh_user".
 *
 * The followings are the available columns in table 'xyh_user':
 * @property string $id
 * @property string $mobile
 * @property string $username
 * @property string $avatar_url
 * @property string $gender
 * @property string $city_id
 * @property string $profession_id
 * @property string $email
 * @property string $hobby
 * @property string $signature
 * @property string $company
 * @property string $reg_phone_type
 * @property string $ctime
 * @property string $mtime
 * @property string $status
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xyh_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('mobile', 'length', 'max'=>11),
			array('username, avatar_url, hobby, signature, company', 'length', 'max'=>255),
                        array('email','email','message'=>'请输入正确的邮箱!'),
                    
                        array('mobile','required','message'=>'请输入手机号码!'),
                        array('mobile','numerical','message'=>'请输入正确的手机号码!'),
                        array('mobile','length', 'max'=>11,'min'=>11,'tooShort'=>'手机号码必须为11位'),
                    
			array('gender, reg_phone_type, status', 'length', 'max'=>1),
			array('city_id, profession_id, ctime, mtime', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, mobile, username, avatar_url, gender, city_id, profession_id, email, hobby, signature, company, reg_phone_type, ctime, mtime, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    
                     'BreakingNews' =>array(self::HAS_MANY,'BreakingNews','user_id'),
                     'Area' => array(self::BELONGS_TO, 'Area', 'city_id'),
                     'Profession'=>array(self::BELONGS_TO,'Profession','profession_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'mobile' => 'Mobile',
			'username' => 'Username',
			'avatar_url' => 'Avatar Url',
			'gender' => 'Gender',
			'city_id' => 'City',
			'profession_id' => 'Profession',
			'email' => 'Email',
			'hobby' => 'Hobby',
			'signature' => 'Signature',
			'company' => 'Company',
			'reg_phone_type' => 'Reg Phone Type',
			'ctime' => 'Ctime',
			'mtime' => 'Mtime',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

               
            
		$criteria=new CDbCriteria;
                
                
                if($this->ctime){
                    $tmp = strtotime($this->ctime);
                    $criteria->addCondition("ctime >=$tmp");
                }
                
               $criteria->addCondition("status=1");
		$criteria->compare('id',$this->id,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('avatar_url',$this->avatar_url,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('profession_id',$this->profession_id,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('hobby',$this->hobby,true);
		$criteria->compare('signature',$this->signature,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('reg_phone_type',$this->reg_phone_type,true);
		
		$criteria->compare('mtime',$this->mtime,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'sort' =>array('defaultOrder'=>'id DESC')
                       
		));
	}
}